package com.example.weblending.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.weblending.domain.statistics.*;
import com.example.weblending.vo.AnalyzeColumn;
import com.example.weblending.vo.CsvInputColumn;

import java.util.*;

public class StatisticsUtils {
    private AnnualIncome annualIncome;
    private InterestRate interestRate;
    private Installment installment;
    private EmploymentLength employmentLength;
    private HomeOwnership homeOwnership;
    private loanAmnt loanAmnt;
    private Dti dti;
    private RevolUtil revolUtil;

    Map<Integer,StatisticsField> statistics;

    public StatisticsUtils(){
       annualIncome=new AnnualIncome();
       interestRate=new InterestRate();
       installment=new Installment();
       employmentLength =new EmploymentLength();
       homeOwnership=new HomeOwnership();
       loanAmnt=new loanAmnt();
       dti =new Dti();
       revolUtil =new RevolUtil();
       statistics=new HashMap<>();
    }
    /**
     * 对每条数据进行统计
     */
    public void statistics(AnalyzeColumn i){
        annualIncome.insert(i);
        interestRate.insert(i);
        installment.insert(i);
        employmentLength.insert(i);
        homeOwnership.insert(i);
        loanAmnt.insert(i);
        dti.insert(i);
        revolUtil.insert(i);
        if(!statistics.containsKey(i.hashCode())){
            StatisticsField field=new StatisticsField("");
            field.setNum(1);
            field.setPercent(i.getIs());
            statistics.put(i.hashCode(),field);
        }else{
            StatisticsField field= statistics.get(i.hashCode());
            field.add(i.getIs());
        }
    }

    /**
     * 根据各个区间进行合并
     */
    public String getConditionStatistic(){
        System.out.println(this.statistics.size());
        return JSON.toJSONString(this.statistics);
    }

    /**
     * 各个条件按照顺序进行统计
     * @return
     */
    public String result(){
        JSONObject field=new JSONObject();
        field.put("annualIncome",annualIncome.result());
        field.put("interestRate",interestRate.result());
        field.put("installment",installment.result());
        field.put("employmentLength",employmentLength.result());
        field.put("homeOwnership",homeOwnership.result());
        field.put("loanAmnt",loanAmnt.result());
        field.put("dti", dti.result());
        field.put("revolUtil",revolUtil.result());
        return JSON.toJSONString(field);
    }

    public String sortResult(String relevance){
        JSONArray result=new JSONArray();
        TreeMap<Float, Integer> tree = new TreeMap<Float,Integer>(new Comparator<Float>() {
            public int compare(Float key1,Float key2)
            {
                //降序排序
                return key2.compareTo(key1);
            }
        });
        for(Integer k:this.statistics.keySet()){
            StatisticsField v=this.statistics.get(k);
            tree.put(v.getPercent()/v.getNum()*100,k);
        }
        List<Integer> sort= new ArrayList<>(tree.values());
        tree=null;
        System.gc();
        result.add(sort.get(0));
        result.add(sort.get(1));
        result.add(sort.get(2));
        result.add(sort.get(sort.size()-1));
        sort=null;
        System.gc();
        return JSON.toJSONString(result);
    }

    /**
     * 根据指定区间获得结果
     */
    public Float getPercent(Map<String,Integer> condition,Map<Integer,StatisticsField> statistics){
        int code=0;
        for (String key:condition.keySet()){
            switch (key){
                case "annualIncome":    code+= condition.get(key);break;
                case "interestRate":    code+= condition.get(key) <<4;break;
                case "installment":     code+= condition.get(key) <<8;break;
                case "employmentLength":code+= condition.get(key) <<12;break;
                case "loanAmnt":        code+= condition.get(key) <<16;break;
                case "dti":             code+= condition.get(key) <<20;break;
                case "revolUtil":       code+= condition.get(key) <<24;break;
                case "homeOwnership":   code+= condition.get(key) <<28;break;
            }
        }
        List<StatisticsField> list=new ArrayList<>();
        for (int key:statistics.keySet()){
            if ((key&code)==code){
                list.add(statistics.get(key));
            }
        }
        if(list.size()>0){
            return null;
        }
        int num=0;
        float percent=0;
        for (StatisticsField field:list){
            num+=field.getNum();
            percent+= field.getPercent();
        }
        return percent/num*100;
    }


}
